#include "chatting.h"
#include "ui_chatting.h"

Chatting::Chatting(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Chatting), l(new Login(this)), sock(new QTcpSocket(this))
{
    ui->setupUi(this);
    sock->connectToHost("120.79.255.109", 8082);
    connect(sock, &QTcpSocket::readyRead, this, &Chatting::readData);
    connect(ui->pushButton_request, &QPushButton::clicked, this, &Chatting::chatRequest);
    connect(ui->pushButton_send, &QPushButton::clicked, this, &Chatting::sendInfo);
    connect(ui->pushButton_groupChat, &QPushButton::clicked, this, &Chatting::groupRequest);
    l->setSocket(sock);
    l->show();
}

Chatting::~Chatting()
{
    delete ui;
}

void Chatting::chatRequest()
{
    QJsonObject obj;
    obj["name"] = "wjz";
    QJsonDocument doc(obj);
    QString data = doc.toJson();
    sendData(data);
}

void Chatting::addControl(const QString& name)
{
    // 添加左侧按钮控件
    QPushButton* btn = new QPushButton(this);
    btn->setText(name);
    btn->setMinimumSize(280, 80);
    ui->verticalLayout_menu->insertWidget(ui->verticalLayout_menu->count() - 1, btn);
    // 添加标题控件
    QWidget* w = new QWidget();
    QWidget* widget_title = new QWidget(w);
    QListWidget* widget_chat = new QListWidget(w);
    widget_title->setGeometry(0, 0, 680, 100);
    widget_chat->setGeometry(0, 100, 680, 470);
    QLabel* title = new QLabel(widget_title);
    title->setGeometry(30, 30, 240, 40);
    title->setText(name);
    ui->stackedWidget->addWidget(w);
    connect(btn, &QPushButton::clicked, [=](){
        ui->stackedWidget->setCurrentIndex(ui->stackedWidget->count() - 1);
    });
    titles.push_back(title);
    chats.push_back(widget_chat);
}

void Chatting::readData()
{
    qDebug() << "readData";
    QString data = sock->readAll();
    qDebug() << data;
    QJsonDocument doc = QJsonDocument::fromJson(data.toStdString().c_str());
    QJsonObject obj = doc.object();
    // 登录结果
    if (!obj["result"].isNull() && obj["name"].isNull())
    {
        l->getResult(obj);
    }
    else if (!obj["msg"].isNull())
    {
        readChatInfo(obj["name"].toString(), obj["msg"].toString());
    }
    else if (!obj["group"].isNull())
    {
        addControl(obj["name"].toString());
    }
    else
    {
        // 对方离线
        if (!obj["result"].isNull() && !obj["name"].isNull())
        {
            ui->label_status->setText("离线");
        }
        // 对方在线
        if (!obj["name"].isNull() && obj["result"].isNull())
        {
            ui->label_status->setText("在线");
            addControl(obj["name"].toString());
        }
    }
}

void Chatting::sendData(const QString& data)
{
    sock->write(data.toStdString().c_str());
}

QString Chatting::sendChatInfo(const QString* name)
{
    // 构建json
    QJsonObject obj;
    obj["name"] = *name;
    obj["msg"] = ui->lineEdit_input->text();
    QJsonDocument doc(obj);
    return doc.toJson();
}

void Chatting::readChatInfo(const QString& name, const QString& msg)
{
    int index = ui->stackedWidget->currentIndex();
    if (index < 1)
    {
        qDebug() << "当前没有在线的好友";
        return;
    }
    QListWidget* lw = chats[index - 1];
    lw->addItem(name + ": " + msg);
}

void Chatting::groupRequest()
{
    QJsonObject obj;
    obj["group"] = true;
    QJsonDocument doc(obj);
    QString data = doc.toJson();
    sendData(data);
}

QString Chatting::sendGroupChatInfo()
{
    // 构建json
    QJsonObject obj;
    obj["msg"] = ui->lineEdit_input->text();
    obj["group"] = true;
    QJsonDocument doc(obj);
    return doc.toJson();
}

void Chatting::sendInfo()
{
    int index = ui->stackedWidget->currentIndex();
    if (index < 1)
    {
        qDebug() << "当前没有在线的好友";
        return;
    }
    QLabel* nameLabel = titles[index - 1];
    QString data;
    if (nameLabel->text() == "搞笑份子")
    {
        data = sendGroupChatInfo();
    }
    else
    {
        QString name = nameLabel->text();
        data = sendChatInfo(&name);
    }
    sendData(data);
    // 将消息显示到聊天框
    qDebug() << ui->lineEdit_input->text();
    QListWidget* lw = chats[index - 1];
    lw->addItem(l->getUser() + ": " + ui->lineEdit_input->text());
    // 清空输入框
    ui->lineEdit_input->setText("");
}
